package com.qch.rabbitmq.demo;

import com.rabbitmq.client.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author qch
 * @Date 2021/5/18
 */
public class Producer {
    public static void main(String[] args)throws Exception {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUri("amqp://root:123456@192.168.142.135:5672/%2f");

        try (Connection connection = connectionFactory.newConnection();
             Channel channel = connection.createChannel()){
            Map<String,Object>arguments=new HashMap<>();
            //消息队列中消息过期时间，30s
            arguments.put("x-message-ttl",10*1000);
            //如果消息队列没有消费者，则10s后消息队列过期，消息队列被删
            arguments.put("x-expires",60*1000);
            channel.queueDeclare("queue.ttl.waiting",true,false,false,arguments);
            channel.exchangeDeclare("ex.ttl.waiting",BuiltinExchangeType.DIRECT, true, false, null);
            channel.queueBind("queue.ttl.waiting", "ex.ttl.waiting", "key.ttl.waiting");
            AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().contentEncoding("utf-8")
                    .deliveryMode(2)//表示持久化消息
                    .build();
            channel.basicPublish("ex.ttl.waiting","key.ttl.waiting",properties,"等待的订单号".getBytes("utf-8"));

        }catch (Exception ex){

        }

    }
}

